#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''eof
name:3年内申请人历史被执行情况
code:IG_EnforceInThree
tableName:
columnName:
groups:准入>企业通用
dependencies:EG
type:常用指标
dataSourceType:在线指标
description:
eof'''

'''
ratingRequest 中legalPerson申请人姓名 legalPersonIdCard 申请人ID
与EG中EG_case 诉讼 zhixing表 中 pname 和 pname_id 分别匹配
'''

import sys
import pandas as pd
import datetime
import re

reload(sys)
sys.setdefaultencoding('utf-8')

last_year_list = [(datetime.datetime.now() - pd.tseries.offsets.DateOffset(years=i)).strftime('%Y-%m-%d')\
                    for i in [3 , 0]]

def Ind_Enforce_3Yr():
    null_type_list = ['', None, 'null', 'Null', 'NULL', '/']
    count_list = set()
    try:
        applicant_name = ratingRequest['legalPerson']
        applicant_id = ratingRequest['legalPersonIdCard']
        case = EG['srcData'][0]['EG_case']['data']

        if len(case) >=1:
            for i in case:
                try:
                    if i.get('bbd_table') not in null_type_list:
                        if i['bbd_table'] == 'zhixing':
                            if Formate(i.get('case_create_time')) is not None and i.get('case_code') not in null_type_list \
                                    and i.has_key('pname') and i.has_key('pname_id'):
                                if (i.get('idtype') == u'身份证' or i['pname'] == applicant_name or \
                                    (i['pname_id'] not in null_type_list and re.compile(i['pname_id'].replace('*', '.*')).match(applicant_id) is not None ))\
                                    and Formate(i['case_create_time']) > last_year_list[0] \
                                    and Formate(i['case_create_time']) <= last_year_list[1]:
                                    count_list.add(Formate(i['case_create_time']) + i['case_code'])
                                else:
                                    count_list.add(0)
                            else:
                                count_list.add(None)
                        else:
                            continue
                    else:
                        count_list.add(None)
                except:
                    count_list.add(None)
                    continue

            if None in count_list and  len(count_list) ==1:
                return u'缺失值'
            else:
                count_notnone = [i for i in count_list if i!=None and i!=0]
                return len(count_notnone)
        else:
            return 0
    except:
        return u'缺失值'

def Formate(time):
    import re
    import datetime
    r = re.compile(r'\D+')
    try:
        date = datetime.datetime.strptime(time, '%Y.%m.%d').strftime('%Y-%m-%d')
    except:
        try:
            date = datetime.datetime.strptime(time, '%Y-%m-%d').strftime('%Y-%m-%d')
        except:
            try:
                date = datetime.datetime.strptime(time, '%Y/%m/%d').strftime('%Y-%m-%d')
            except:
                try:
                    tmp = r.sub('', time)
                    date = datetime.datetime.strptime(tmp, '%Y%m%d').strftime('%Y-%m-%d')
                except:
                    date = None
    return date

result = Ind_Enforce_3Yr()